package cn.com.callback;

import java.util.Map;
import cn.com.elite.EliteBean;
import cn.com.elite.PropertiesUtil;
import cn.com.elite.Util;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import net.sf.json.JSONObject;
import net.sf.json.JSONArray;

/**
 * @author ThinkPad
 *
 */
public class wxInterfacePools implements ServiceBackInvoke {
    private static final Log log = LogFactory.getLog(wxInterfacePools.class);

    public String doCallBack(EliteBean eliteBean, HttpServletRequest request, HttpServletResponse response) {
        PropertiesUtil properties = new PropertiesUtil();
        def ecservletip = properties.readValue("ecservletip"); //获取配置文件ec接口IP
        def appservletip = properties.readValue("appservletip"); //获取配置文件session校验接口IP
        def crmservletip = properties.readValue("crmservletip");//获取配置文件CRM接口IP
        def ecsecretkey = properties.readValue("ecsecretkey");//获取配置文件ec接口加密Key
        def appsecretkey = properties.readValue("appsecretkey");//获取配置文件session校验接口加密Key
        def usercode = ""; //初始化客户编号
        def querysql = ""; //初始化查询sql
        def sqlresult = ""; //初始化sql结果
        def url = ""; //初始化接口调用地址
        def rownum = 0;
        def sessionid = Util.filterForObject(request.getParameter("sessionid")); //获取sessionid
        def requestbody = "{\"n\":\"SessionIdCheck\",\"s\":\"" + sessionid + "\",\"q\":{\"t\":\"" + Util.getNowString("yyyy-mm-dd HH:mm:ss") + "\"}}" //入参报文
        appsecretkey = Util.getMD5Str(requestbody + appsecretkey); //MD5加密
        JSONObject obj = JSONObject.fromObject(requestbody); //json解析
        obj.put("m", appsecretkey);
        try {
            def charset = "utf-8";
            def timeout = 100000;
            def enableSSLflag = 0;
            def requestinfo = Util.doHttpPost(appservletip, JSONObject.fromObject(obj).toString(), charset, timeout, enableSSLflag); //EC会员校验接口
            obj = JSONObject.fromObject(requestinfo);
            if (obj.getJSONObject("q").getInt("s") > 0) {
                return "{\"state\" :\"0\" , \"success\" : false,\"result\" :{} ,\"msg\" :\"" + obj.getJSONObject("q").getString("d") + "\"}";
            } else {
                usercode = obj.getJSONObject("q").getString("uid");
            };
            querysql = "select customer_guid from customer where customer_id='" + usercode + "'";
            sqlresult = eliteBean.chaxun_sql(querysql, "dataSource");
            rownum = sqlresult.size; //获取sql返回行数
            if (rownum == 0) {
                requestinfo = "{\"flag\": \"ECCRM\",\"method\": \"userinfo\",\"type\": \"json\",\"charset\": \"utf-8\",\"ver\":1,\"timestamp\": \"\",\"content\": {\"USER_CODE\":\"" + usercode + "\"},\"sign\": \"\",\"secretkey\": \"" + ecsecretkey + "\"}";
                url = crmservletip + "/ensign";
                requestinfo = Util.doHttpPost(url, requestinfo, charset, timeout, enableSSLflag);
                requestinfo = Util.doHttpPost(ecservletip, requestinfo, charset, timeout, enableSSLflag);
                obj = JSONObject.fromObject(JSONObject.fromObject(requestinfo).getJSONArray("content").getString(0));//解析json报文
                log.debug('obj:' + obj);
                usercode = obj.getString("USER_CODE");
                if (usercode != "") {
                    querysql = "insert into customer(customer_guid,customer_id,customername,mobile,customertype_id,idtype,customerid,stringfield12,stringfield14,stringfield15,createdby,createddate,relcodetable)values(newid(),'" + Util.filterForObject(obj.getString("USER_CODE")) + "','" + Util.filterForObject(obj.getString("USER_NAME")) + "','" + Util.filterForObject(obj.getString("MOBILETELE")) + "','" + Util.filterForObject(obj.getString("USER_TYPE")) + "','" + Util.filterForObject(obj.getString("PAPERTYPE")) + "','" + Util.filterForObject(obj.getString("PAPERNUMBER")) + "','" + Util.filterForObject(obj.getString("STATE")) + "','" + Util.filterForObject(obj.getString("CFW_TYPE")) + "','" + Util.filterForObject(obj.getString("REMARK")) + "','SELITE',getdate(),'wechat_i')";
                    sqlresult = eliteBean.update(querysql, "dataSource");
                    return "{\"state\" :\"1\" , \"success\" : true,\"result\" :{\"loginName\" :\"" + usercode + "\",\"wechatname\" :\"\",\"password\" :\"\"} ,\"msg\" :\"接口调用成功\"}"
                } else {
                    return "{\"state\" :\"0\" , \"success\" : false,\"result\" :{} ,\"msg\" :\"获取不到会员编号\"}";
                }
            } else {
                return "{\"state\" :\"1\" , \"success\" : true,\"result\" :{\"loginName\" :\"" + usercode + "\",\"wechatname\" :\"\",\"password\" :\"\"} ,\"msg\" :\"接口调用成功\"}"
            };
        } catch (Exception e) {
            log.error("", e);
            return "{\"state\" :\"0\" , \"success\" : false,\"result\" :{} ,\"msg\" :\"接口调用失败,获取不到会员编号\"}";
        };

    }
}
